package com.wego.controller;

import com.wego.bean.StateBean;
import com.wego.bean.TableBean;
import com.wego.bean.TrackingBean;
import com.wego.common.bean.PageBean;
import com.wego.common.formatter.LocalDateTimeFormatter;
import com.wego.constant.WeGoConstant;
import com.wego.entity.domain.Order;
import com.wego.entity.query.OrderQuery;
import com.wego.entity.vo.OrderVO;
import com.wego.service.AddressService;
import com.wego.service.OrderService;
import com.wego.service.PayMethodService;
import com.wego.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;


/**
 * (tb_order)表控制层
 *
 * @author Li Chuanwei
 */
@Controller
@RequestMapping("/manager/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private PayMethodService payMethodService;

    @Autowired
    private UserService userService;

    @Autowired
    private AddressService addressService;

    @GetMapping("/list")
    public @ResponseBody TableBean list(OrderQuery orderQuery) {
        if(orderQuery.getStartTime() != null && orderQuery.getStartTime().equals(""))
            orderQuery.setStartTime(null);
        if(orderQuery.getEndTime() != null && orderQuery.getEndTime().equals(""))
            orderQuery.setEndTime(null);
        final PageBean<Order> orderPageBean = orderService.selectPage(orderQuery);
        PageBean<OrderVO> pageBean = orderPageBean2OrderVOPageBean(orderPageBean);
        List<OrderVO> orderVOS = pageBean.getResult();
        Long count = pageBean.getTotal();
        TableBean tableBean = new TableBean<OrderVO>();
        tableBean.setCode(0);
        tableBean.setMsg("");
        tableBean.setData(orderVOS);
        tableBean.setCount(count);
        return tableBean;
    }

    private PageBean<OrderVO> orderPageBean2OrderVOPageBean(PageBean<Order> pageBean) {
        PageBean<OrderVO> orderVOPageBean = new PageBean<>();
        orderVOPageBean.setPageNum(pageBean.getPageNum());
        orderVOPageBean.setPageSize(pageBean.getPageSize());
        orderVOPageBean.setPages(pageBean.getPages());
        orderVOPageBean.setTotal(pageBean.getTotal());

        List<OrderVO> list = new ArrayList<>();
        for (Order order : pageBean.getResult()) {
            final OrderVO orderVO = order2OrderVO(order);
            list.add(orderVO);
        }
        orderVOPageBean.setResult(list);

        return orderVOPageBean;
    }

    private OrderVO order2OrderVO(Order order) {
        if (order == null) {
            return null;
        }
        OrderVO orderVO = new OrderVO();
        orderVO.setId(order.getId());
        orderVO.setAmount(order.getAmount());
        orderVO.setMoney(order.getMoney());
        if(order.getPayMethodId() != null)
            orderVO.setPayMethodName(payMethodService.selectByPrimaryKey(order.getPayMethodId()).getName());
        orderVO.setUserId(order.getUserId());
        if(order.getUserId() != null)
            orderVO.setUserName(userService.selectByPrimaryKey(order.getUserId()).getNickname());
        if(order.getAddressId() != null)
            orderVO.setAddressName(addressService.selectAddress(order.getAddressId()));
        orderVO.setTrackingName(order.getTrackingName());
        orderVO.setTrackingNumber(order.getTrackingNumber());
        orderVO.setPriority(order.getPriority());
        orderVO.setState(WeGoConstant.ORDER_STATE.get(order.getState()));
        orderVO.setCreateTime(LocalDateTimeFormatter.formatLocalDateTime(order.getCreateTime(),"yyyy-MM-dd HH:mm:ss"));
        orderVO.setUpdateTime(LocalDateTimeFormatter.formatLocalDateTime(order.getUpdateTime(),"yyyy-MM-dd HH:mm:ss"));
        return orderVO;
    }

    @GetMapping("/updateTrackingNumber")
    public @ResponseBody StateBean updateTrackingNumber(Order order){
        StateBean success = new StateBean(200,"成功");
        StateBean fail = new StateBean(0,"失败");
        if(order == null || order.getId() == null)
            return fail;
        order.setState(3);
        order.setUpdateTime(LocalDateTime.now());
        if(orderService.updateByPrimaryKeySelective(order))
            return success;
        else
            return fail;
    }

    @GetMapping("/tracking")
    public String map(String orderId, Model model){
        model.addAttribute("orderId",orderId);
        return "manager/tracking_details";
    }

    @GetMapping("/trackingDetails")
    public @ResponseBody  TableBean<TrackingBean> trackingDetails(String orderId, Model model){
        Order order = orderService.selectByPrimaryKey(orderId);
        String name = order.getTrackingName();
        String id = order.getTrackingNumber();
        List<TrackingBean> list = new ArrayList<>();
        list.add(new TrackingBean(name,id));
        return new TableBean<TrackingBean>(0,"",1L,list);
    }
}
